<template>
  <div>
    <!--    面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/administration' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>账号管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <!--      搜索添加-->
      <el-row :gutter="20">
        <el-col :span="13">
          <el-button type="primary" @click="UserVisible = true">添加用户</el-button>
        </el-col>
      </el-row>

      <!--    用户列表-->
      <el-table border :data="userData" style="width: 100%">
        <el-table-column
                type="index"
                width="50">
        </el-table-column>
        <el-table-column
                prop="username"
                label="用户名">
        </el-table-column>
        <el-table-column
                prop="email"
                label="邮箱">
        </el-table-column>
        <el-table-column
                prop="identity"
                label="身份">
        </el-table-column>
        <el-table-column
                prop="mobile"
                label="电话">
        </el-table-column>
        <el-table-column
                prop="date"
                label="日期">
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="openEditDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUser(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--      分页-->
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[3, 5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="user.length">
      </el-pagination>

    </el-card>

    <!--添加用户对话框-->
    <el-dialog
            title="提示"
            :visible.sync="UserVisible"
            width="30%"
            @close="handleClose('add')">

      <!--      form表单-->
      <el-form :model="userForm" :rules="userRules" ref="userRuleForm" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="userForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input type="text" v-model="userForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input type="text" v-model="userForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="身份" prop="identity">
          <el-select v-model="userForm.identity" placeholder="请选择身份">
            <el-option
                    v-for="item in identity"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="UserVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
      </span>
    </el-dialog>

    <!--修改用户对话框-->
    <el-dialog
            title="提示"
            :visible.sync="singleUserVisible"
            width="30%"
            @close="handleClose('edit')">

      <!--      form表单-->
      <el-form :model="singleUserForm" :rules="userRules" ref="singleUserRuleForm" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="singleUserForm.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input type="text" v-model="singleUserForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input type="text" v-model="singleUserForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="身份" prop="identity">
          <el-select v-model="singleUserForm.identity" placeholder="请选择身份">
            <el-option
                    v-for="item in identity"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="singleUserVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import {mapState} from 'vuex'

  export default {
    name: "User",
    data() {
      return {
        userData: [],
        UserVisible: false,
        singleUserVisible: false,
        userForm: {
          username: '',
          email: '',
          mobile: '',
          password: '',
          identity: ''
        },
        singleUserForm: {
          username: '',
          email: '',
          mobile: '',
          identity: ''
        },
        userRules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'},
            {min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur'}
          ],
          email: [
            {required: true, message: '请输入邮箱', trigger: 'blur'},
          ],
          mobile: [
            {required: true, message: '请输入电话', trigger: 'blur'},
          ],
          identity: [
            {required: true, message: '请选择身份', trigger: 'blur'},
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
          ],
        },
        identity: [{
          value: '普通用户',
          label: '普通用户',
        }, {
          value: '管理员',
          label: '管理员',
        }],
        currentPage: 1,
        pageSize: 5
      }
    },
    methods: {
      getList() {
        this.$store.dispatch('getUser').then(() => {
          this.handleCurrentChange(this.currentPage)
          if (this.userData.length === 0 && this.currentPage > 0) {
            this.currentPage--
            this.handleCurrentChange(this.currentPage)
          }
        })
      },
      submitAddForm() {
        this.$refs.userRuleForm.validate((valid) => {
          if (valid) {
            this.$store.dispatch('addUser', this.userForm)
              .then(() => {
                this.UserVisible = false
                this.getList()
                this.$message({
                  message: '添加用户成功',
                  type: 'success'
                })
              })
              .catch((err) => {
                this.$message.error(err.data.user)
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      removeUser(row) {
        this.$store.dispatch('removeUser', {id: row._id})
          .then(() => {
            this.getList()
            this.$message({
              message: '删除用户成功',
              type: 'success'
            })
          })
      },
      handleClose(sign) {
        if (sign === 'add') {
          this.$refs.userRuleForm.resetFields()
        } else {
          this.$refs.singleUserRuleForm.resetFields()
        }
      },
      submitEditForm() {
        this.$refs.singleUserRuleForm.validate((valid) => {
          if (valid) {
            this.$store.dispatch('editUser', this.singleUserForm)
              .then(() => {
                this.singleUserVisible = false
                this.getList()
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      openEditDialog(row) {
        this.$store.dispatch('searchSingleUser', row._id)
          .then(() => {
            this.singleUserForm = this.$store.state.singleUser
            console.log(this.singleUserForm)
          })
        this.singleUserVisible = true
      },
      handleSizeChange(size) {
        this.pageSize = size
        this.handleCurrentChange(this.currentPage)
      },  //切换条数
      handleCurrentChange(page) {
        this.currentPage = page
        this.userData = this.$store.state.user.filter((item, index) => index >= this.pageSize * (this.currentPage - 1) && index < this.pageSize * this.currentPage)
      },  //切换页数
    },
    created() {
      this.getList()
    },
    computed: {
      ...mapState(['user'])
    }
  }
</script>

<style scoped>
  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .el-table {
    margin-top: 20px;
  }
</style>